package cinvestav.adviseme.bean;

import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

import org.icefaces.component.fileentry.FileEntry;
import org.icefaces.component.fileentry.FileEntryEvent;
import org.icefaces.component.fileentry.FileEntryResults;

import cinvestav.adviseme.controller.IPersonaController;
import cinvestav.adviseme.controller.PersonaController;
import cinvestav.adviseme.databasemodel.LugarAgregadoDB;
import cinvestav.adviseme.datamodel.Persona;
import cinvestav.adviseme.util.DBException;

public class RegisterBean {
	private static Logger log = Logger.getLogger(LugarAgregadoDB.class.getName());
	protected IPersonaController personaController;
	protected Persona nuevaPersona;
	protected String confirmaPassword;
	protected Date fechaNacimiento;

	public RegisterBean() throws DBException {
		super();
		personaController = new PersonaController();
		nuevaPersona = new Persona();
		// TODO Auto-generated constructor stub
	}

	public IPersonaController getPersonaController() {
		return personaController;
	}

	public void setPersonaController(IPersonaController personaController) {
		this.personaController = personaController;
	}

	public Persona getNuevaPersona() {
		return nuevaPersona;
	}

	public void setNuevaPersona(Persona nuevaPersona) {
		this.nuevaPersona = nuevaPersona;
	}
	
	public Date getFechaNacimiento() {
		return fechaNacimiento;
	}

	public void setFechaNacimiento(Date fechaNacimiento) {
		this.fechaNacimiento = fechaNacimiento;
	}

	/**
	 * Gets the default timezone of the host server. The timezone is needed by
	 * the convertDateTime for formatting the time dat values.
	 * 
	 * @return timezone for the current JVM
	 */
	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public String getConfirmaPassword() {
		return confirmaPassword;
	}

	public void setConfirmaPassword(String confirmaPassword) {
		this.confirmaPassword = confirmaPassword;
	}

	/**
	 * Validacion para el password, verifica que ambos password sean iguales
	 * 
	 * @param context
	 * @param validate
	 * @param value
	 */
	public void validaPassword(FacesContext context, UIComponent validate,
			Object value) {
		String password = (String) value;

		log.log(Level.INFO,"PASS = "+password + " - "+nuevaPersona.getPassword());
		// Verificar si los password insertados son iguales

		if (!password.equals(nuevaPersona.getPassword())) {
			((UIInput) validate).setValid(false);
			FacesMessage msg = new FacesMessage(
					"Las contraseñas deben coincidir");
			context.addMessage(validate.getClientId(context), msg);

		}
	}

	public void registrar() {
		log.log(Level.INFO,"PERSONA= "+nuevaPersona);
		try {
			personaController.insert(nuevaPersona);
			nuevaPersona = new Persona();
			fechaNacimiento = new Date();
			confirmaPassword = "";
		} catch (DBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void listener(FileEntryEvent event) {
	    FileEntry fileEntry = (FileEntry) event.getSource();
	    FileEntryResults results = fileEntry.getResults();
	    for (FileEntryResults.FileInfo fileInfo : results.getFiles()) {
	        if (fileInfo.isSaved()) {
	        	String filePath = fileInfo.getFile().getAbsolutePath();
	        	filePath.substring(filePath.lastIndexOf("pics"));
	        	nuevaPersona.setFotografia(filePath.substring(filePath.lastIndexOf("pics")-1));
	        }
	    }
	    
	}


}
